package plugins.search;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.BillOperationStatus;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.DistributeSessionlessCache;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Search;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;

/**
 * 动态表单插件
 */
public class SearchPlugin extends AbstractFormPlugin implements SearchEnterListener {
    // 搜索控件标识
    private final static String KEY_SEARCH = "dcb4_searchap";

    @Override
    public void registerListener(EventObject e) {
        // 侦听搜索控件的搜索事件
        Search search = getView().getControl(KEY_SEARCH);
        search.addEnterListener(this);
        super.registerListener(e);
    }

    @Override
    public void search(SearchEnterEvent evt) {
        Search search = (Search) evt.getSource();
        if (StringUtils.equals(KEY_SEARCH, search.getKey())){
            String searchText = evt.getText();
            this.doSearch(searchText);
        }
    }

    /**
     * 实现搜索
     * @param searchText	搜索文本
     */
    private void doSearch(String searchText) {
        ListShowParameter lsp = new ListShowParameter();
        lsp.setFormId("dcb4_dishes");
        lsp.setBillFormId("dcb4_dishes_list");
        lsp.getOpenStyle().setShowType(ShowType.Modal);
        ListFilterParameter filter = new ListFilterParameter();
        QFilter qFilter = new QFilter("name", QCP.like, "%" + searchText + "%");
        filter.setQFilter(String.valueOf(qFilter));
        lsp.setListFilterParameter(filter);
        this.getView().showForm(lsp);



    }


    public List<String> getSearchList(SearchEnterEvent evt) {
        // 自动补全风格 输入搜索条件后 进入该方法组织数据返回前端显示
        Search search = (Search) evt.getSource();

        if (StringUtils.equals("dcb4_searchap", search.getKey())) {
            // 获取搜索条件
            String searchText = search.getSearchKey();
            if (StringUtils.isNotBlank(searchText)) {
                return doSearchList(searchText);
            }

        }

        return null;
    }

    private List<String> doSearchList(String searchText) {

        String fields = "name";
        Map<String, String> searchList = new HashMap<String, String>();

        QFilter filter = new QFilter("name", QCP.equals, searchText);

        // 查询数据
        DynamicObject[] objects = BusinessDataServiceHelper.load("dcb4_dishes", "id", new QFilter[]{filter});

        for (DynamicObject obj : objects) {
            String name = obj.getString("name");
            if (StringUtils.isNotBlank(name)) {
                searchList.put("dcb4_dishes" + " " + obj.get("id").toString(), name);
            }
        }

        return new ArrayList<>(searchList.values());
    }


}